package com.solotiger.douban.util;

import android.content.Context;
import android.os.Build;
import android.support.annotation.ColorInt;

import com.solotiger.douban.MyApplication;
import com.solotiger.douban.ui.view.divider.Api20ItemDivider;
import com.solotiger.douban.ui.view.divider.Api21ItemDivider;
import com.solotiger.douban.ui.view.divider.Divider;

/**
 * 显示像素与dip、sp的转换方法
 */
public class DisplayUtil {
    private static final float ADJUST_VALUE = 0.5f;

    /**
     * 将px值转换为dip值
     */
    public static int px2dip(Context context, float pxValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (pxValue / scale + ADJUST_VALUE);
    }

    /**
     * 将dip值转换为px值
     */
    public static int dip2px(Context context, float dipValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dipValue * scale + ADJUST_VALUE);
    }

    /**
     * 获取状态栏高度(px)
     */
    public static int getStatusBarHeight(Context context) {
        int result = 0;
        int resourceId = context.getResources().getIdentifier("status_bar_height", "dimen", "android");
        if (resourceId > 0) {
            result = context.getResources().getDimensionPixelSize(resourceId);
        }
        return result;
    }

    public static Divider getDivider(@ColorInt int color) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            int size = DisplayUtil.dip2px(MyApplication.getInstance(), 6);
            return new Api21ItemDivider(color, size, size);
        } else {
            int size = DisplayUtil.dip2px(MyApplication.getInstance(), 2);
            return new Api20ItemDivider(color, size, size);
        }
    }
}
